var options = [
	{
		browserName: "Edge",
		lowest: "79.0.309.66",
	},
	{
		browserName: "Chrome",
		lowest: "71.0.3578.80",
	},
	{
		browserName: "Firefox",
		lowest: "67.0",
	},
	{
		browserName: "360",
		lowest: "12",
	},
];

(function handleToJudgeShadowShow() {
	handleToChangeModalShow(isBrowserModalShow());
	domBinding();
})();

function domBinding() {
	var chrome = document.getElementById("browser-modal-list-item-chrome");
	var firefox = document.getElementById("browser-modal-list-item-firefox");
	var edge = document.getElementById("browser-modal-list-item-edge");
	var browser360 = document.getElementById("browser-modal-list-item-360");
	var confirm = document.getElementById("browser-modal-confirm");

	var downloadLinkMap = {
		chrome: "https://www.google.cn/intl/zh-CN/chrome/",
		firefox: "https://www.firefox.com.cn/download/#product-desktop-release",
		edge: "https://www.microsoft.com/zh-cn/edge",
		browser360: "https://browser.360.cn/se/",
	};

	chrome.onclick = function () {
		window.open(downloadLinkMap.chrome, "_blank");
	};
	firefox.onclick = function () {
		window.open(downloadLinkMap.firefox, "_blank");
	};
	edge.onclick = function () {
		window.open(downloadLinkMap.edge, "_blank");
	};
	browser360.onclick = function () {
		window.open(downloadLinkMap.browser360, "_blank");
	};
	confirm.onclick = function () {
		handleToChangeModalShow(false);
	};
}

// 控制弹窗显隐
function handleToChangeModalShow(isShow) {
	var shadowDom = document.getElementById("browser-modal-shadow");
	if (isShow) {
		shadowDom.classList.add("browser-modal-shadow-show");
	} else {
		shadowDom.classList.remove("browser-modal-shadow-show");
	}
}

// 判断弹窗是否展示
function isBrowserModalShow() {
	var msg = getBrowserNameVersion();
	console.log(msg);

	var nameArr = [];
	for (var i = 0; i < options.length; i++) {
		var item = options[i].browserName;
		nameArr.push(item);
	}
	var index = nameArr.indexOf(msg.name);

	// var index = options.map((item) => item.browserName).indexOf(msg.name);
	if (index < 0) {
		return true;
	}

	var isCover = isVersionCover(msg.version, options[index].lowest);
	return !isCover;
}

// 获取浏览器名称及其版本号
function getBrowserNameVersion() {
	var Sys = {};
	var ua = navigator.userAgent.toLowerCase();
	var s;
	console.log("ua:", ua);
	(s = ua.match(/rv:([\d.]+)\) like gecko/))
		? (Sys.ie = s[1])
		: (s = ua.match(/edg\/([\d\.]+)/))
		? (Sys.edge = s[1])
		: (s = ua.match(/firefox\/([\d\.]+)/))
		? (Sys.firefox = s[1])
		: (s = ua.match(/chrome\/([\d\.]+)/))
		? (Sys.chrome = s[1])
		: (s = ua.match(/version\/([\d\.]+).*safari/))
		? (Sys.safari = s[1])
		: 0;

	if (Sys.edge) {
		return {
			name: "Edge",
			version: Sys.edge,
		};
	}
	if (Sys.firefox) {
		return {
			name: "Firefox",
			version: Sys.firefox,
		};
	}
	if (Sys.chrome) {
		var cur = "Chrome";
		var mime_types = navigator.mimeTypes || [];
		for (let i = 0; i < mime_types.length; i++) {
			if (mime_types[i].type.indexOf("360") >= 0) {
				cur = "360";
				break;
			}
		}
		return {
			name: cur,
			version: Sys.chrome,
		};
	}
	return {
		name: "other",
		version: null,
	};
}

// 当前版本是否覆盖最低版本号
function isVersionCover(cur, lowest) {
	if (cur === lowest) {
		return true;
	}
	var curArr = cur.split(".");
	var lowestArr = lowest.split(".");

	var length = Math.min(curArr.length, lowestArr.length);

	if (curArr.length < lowestArr.length) {
		for (var i = 0; i < lowestArr.length - curArr.length; i++) {
			curArr.push(0);
		}
	}
	if (curArr.length > lowestArr.length) {
		for (var i = 0; i < curArr.length - lowestArr.length; i++) {
			lowestArr.push(0);
		}
	}

	for (var i = 0; i < curArr.length; i++) {
		var c = Number(curArr[i]);
		var l = Number(lowestArr[i]);

		if (c > l) {
			return true;
		} else if (c < l) {
			return false;
		}
		continue;
	}
}
